﻿Imports System.IO
Imports System.Net.Sockets
Imports System.Threading

Public Class ConnectForm

    Private connectedClient As ConnectedClient
    Private mastermind As Mastermind

    Public Property MyConnectedClient() As ConnectedClient
        Get
            Return connectedClient
        End Get
        Set(ByVal value As ConnectedClient)
            connectedClient = value
        End Set
    End Property

    Public Property MyMastermind() As Mastermind
        Get
            Return mastermind
        End Get
        Set(ByVal value As Mastermind)
            mastermind = value
        End Set
    End Property

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim client As New TcpClient

        Try
            client.Connect(TextBox1.Text, TextBox2.Text)
        Catch ex As Exception
            MsgBox("Failed to connect")
            Return
        End Try

        connectedClient = New ConnectedClient(client)

        Dim data As String = ""
        data = connectedClient.ReadData()

        If data = "CodeMaker" Then
            mastermind = New Mastermind()
            CodeMaker.Show()
            StartThread()
            Me.Hide()
        ElseIf data = "CodeBreaker" Then
            mastermind = New Mastermind()
            CodeBreaker.Show()
            StartThread()
            Me.Hide()
        ElseIf data = "Rejected" Then
            MsgBox("Server is full. Try again later")
        End If
    End Sub

    Private Sub StartThread()
        Dim thread As New Thread(AddressOf StartReading)
        thread.Start(connectedClient)
    End Sub

    Private Sub StartReading(ByVal client As ConnectedClient)
        Dim data As String = ""

        Do
            data = client.ReadData()

            If data <> Nothing Then
                If data = "Start" Then
                    Me.Invoke(New EnableUIDelegate(AddressOf EnableUI))
                ElseIf data = "Next round" Then
                    Me.Invoke(New NextRoundDelegate(AddressOf NextRound))
                ElseIf data = "You won!" Then
                    Me.Invoke(New WonDelegate(AddressOf Won))
                ElseIf data = "You lost" Then
                    Me.Invoke(New LostDelegate(AddressOf Lost))
                ElseIf data = "Tie" Then
                    Me.Invoke(New TieDelegate(AddressOf Tie))
                ElseIf data.Contains("Score") Then
                    Me.Invoke(New GetScoreDelegate(AddressOf GetScore), data)
                Else
                    Me.Invoke(New DecodeDelegate(AddressOf Decode), data)
                End If
            End If
        Loop While client.isConnected()
    End Sub

    Private Delegate Sub EnableUIDelegate()

    Private Sub EnableUI()
        If CodeMaker.Visible Then
            CodeMaker.EnableUI()
        Else
            CodeBreaker.EnableUI()
        End If
    End Sub

    Private Delegate Sub GetScoreDelegate(ByVal data As String)

    Private Sub GetScore(ByVal data As String)
        Dim words() As String = data.Split(New Char() {"."c})

        If Convert.ToInt32(words(1)) > mastermind.GetScore() Then
            connectedClient.SendData("You won!")
            connectedClient.SendData("Exit")
            connectedClient.ShutDownClient()
            MsgBox("You lost")
            Me.Close()
        ElseIf Convert.ToInt32(words(1)) < mastermind.GetScore() Then
            connectedClient.SendData("You lost")
            connectedClient.SendData("Exit")
            connectedClient.ShutDownClient()
            MsgBox("You won")
            Me.Close()
        Else
            connectedClient.SendData("Tie")
            connectedClient.SendData("Exit")
            connectedClient.ShutDownClient()
            MsgBox("Tie")
            Me.Close()
        End If
    End Sub

    Private Delegate Sub NextRoundDelegate()

    Private Sub NextRound()
        CodeBreaker.NextRound()
    End Sub

    Private Delegate Sub WonDelegate()

    Private Sub Won()
        connectedClient.SendData("Exit")
        connectedClient.ShutDownClient()
        MsgBox("You Won!")
        Me.Close()
    End Sub

    Private Delegate Sub LostDelegate()

    Private Sub Lost()
        connectedClient.SendData("Exit")
        connectedClient.ShutDownClient()
        MsgBox("You lost")
        Me.Close()
    End Sub

    Private Delegate Sub TieDelegate()

    Private Sub Tie()
        connectedClient.SendData("Exit")
        connectedClient.ShutDownClient()
        MsgBox("Tie")
        Me.Close()
    End Sub

    Private Delegate Sub DecodeDelegate(ByVal data As String)

    Private Sub Decode(ByVal data As String)
        If CodeMaker.Visible Then
            CodeMaker.Decode(data)
        Else
            CodeBreaker.Decode(data)
        End If
    End Sub
End Class
